<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Post_import extends CI_Controller {
	
	/**
	 * 
	 * The constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->load->model('product_model');
		$this->load->model('site_config_model');
		$this->load->model('lang_general_model');
				
	}
	
	public function get_dates(){
		$configObj = new Site_config_model();
		$langObj = new Lang_general_model();
		
		$shapes = $this->product_model->getAll(array('group_by' => 'shape', 'order' => 'shape', 'field' => 'shape'))->result();
		$shape_string = '';
		
		foreach ($shapes as $shape){
			if ($shape->shape != ''){
				$shape_string .= str_replace(' ', '_', strtolower($shape->shape)) . ';';
				$lang_item = $this->lang_general_model->getAll(array('key' => str_replace(' ', '_', strtolower($shape->shape))))->result();
			
				if (!empty($lang_item)){
					foreach($lang_item[0] as $key=>$value){
						$langObj->$key = $value;
					}
					$langObj->key = str_replace(' ', '_', strtolower($shape->shape));
					$langObj->en = $shape->shape;
					$langObj->update();
				} else {
					$langObj->key = str_replace(' ', '_', strtolower($shape->shape));
					$langObj->en = $shape->shape;
					
					$langObj->insert();
				}
			}
		}
		if (strlen($shape_string)){
			$shape_string = substr($shape_string, 0,-1);
		}

		$configObj->config_key = 'shape_filter';
		$configObj->config_value = $shape_string;

		$item = $this->site_config_model->getByKey('shape_filter');

		if ($item === FALSE){
			$configObj->insert();
		} else {
			$configObj->update();
		}
		
		$configObj = new Site_config_model();
		
		$qualitys = $this->product_model->getAll(array('group_by' => 'clarity', 'order' => 'clarity', 'field' => 'clarity'))->result();
		$quality_string = '';
		foreach ($qualitys as $quality){
			if ($quality->clarity != ''){
				$quality_string .= str_replace(' ', '_', strtolower($quality->clarity)) . ';';
			}
		}
		if (strlen($quality_string)){
			$quality_string = substr($quality_string, 0,-1);
		}
		
		$configObj->config_key = 'quality_filter';
		$configObj->config_value = $quality_string;

		$item = $this->site_config_model->getByKey('quality_filter');
		
		if ($item === FALSE){
			$configObj->insert();
		} else {
			$configObj->update();
		}
		
		$configObj = new Site_config_model();
		$langObj = new Lang_general_model();
		
		$settings = $this->product_model->getAll(array('group_by' => 'setting', 'order' => 'setting', 'field' => 'setting'))->result();
		$setting_string = '';
		foreach ($settings as $setting){
			if ($setting->setting != ''){
				$setting_string .= str_replace(' ', '_', strtolower($setting->setting)) . ';';
		
				$lang_item = $this->lang_general_model->getAll(array('key' => str_replace(' ', '_', strtolower($setting->setting))))->result();
				if (!empty($lang_item)){
					foreach($lang_item[0] as $key=>$value){
						$langObj->$key = $value;
					}
					$langObj->key = str_replace(' ', '_', strtolower($setting->setting));
					$langObj->en = $setting->setting;
					$langObj->update();
				} else {
					$langObj->key = str_replace(' ', '_', strtolower($setting->setting));
					$langObj->en = $setting->setting;
					
					$langObj->insert();
				}
			}
		}
		if (strlen($setting_string)){
			$setting_string = substr($setting_string, 0,-1);
		}
		
		$configObj->config_key = 'setting_filter';
		$configObj->config_value = $setting_string;

		$item = $this->site_config_model->getByKey('setting_filter');
		
		if ($item === FALSE){
			$configObj->insert();
		} else {
			$configObj->update();
		}
		
		$configObj = new Site_config_model();
		$langObj = new Lang_general_model();
		
		$colours = $this->product_model->getAll(array('group_by' => 'colour', 'order' => 'colour', 'field' => 'colour'))->result();
				
		$color_string = '';
		foreach ($colours as $colour){
			if ($colour->colour != ''){
				$color_string .= str_replace(' ', '_', strtolower($colour->colour)) . ';';
		
				$lang_item = $this->lang_general_model->getAll(array('key' => strtolower($colour->colour) . '_color'))->result();
				if (!empty($lang_item)){
					foreach($lang_item[0] as $key=>$value){
						$langObj->$key = $value;
					}
					$langObj->key = strtolower($colour->colour) . '_color';
					switch (strtolower($colour->colour)){
						case "d":
							$langObj->en = "Exceptional White +";
							break;
						case "e":
							$langObj->en = "Exceptional White";
							break;
						case "f":
							$langObj->en = "Rare white / Colourless";
							break;
						case "g":
							$langObj->en = "Rare white / Near Colourless";
							break;
						case "h":
							$langObj->en = "White / Near Colourless";
							break;
						case "i":
							$langObj->en = "Near Colourless";
							break;
						default:
							$langObj->en = $colour->colour;
							break;
					}
					$langObj->update();
				} else {
					$langObj->key = strtolower($colour->colour) . '_color';
					switch (strtolower($colour->colour)){
						case "d":
							$langObj->en = "Exceptional White +";
							break;
						case "e":
							$langObj->en = "Exceptional White";
							break;
						case "f":
							$langObj->en = "Rare white / Colourless";
							break;
						case "g":
							$langObj->en = "Rare white / Near Colourless";
							break;
						case "h":
							$langObj->en = "White / Near Colourless";
							break;
						default:
							$langObj->en = $colour->colour;
							break;
					}
					$langObj->insert();
				}
			}
		}
		if (strlen($color_string)){
			$color_string = substr($color_string, 0,-1);
		}
		
		$configObj->config_key = 'color_filter';
		$configObj->config_value = $color_string;

		$item = $this->site_config_model->getByKey('color_filter');
		
		if ($item === FALSE){
			$configObj->insert();
		} else {
			$configObj->update();
		}
	}
}